package com.pearl.generator.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.pearl.generator.common.Result;
import com.pearl.generator.pojo.entity.DataSourceEntity;
import com.pearl.generator.pojo.request.GeneratorCodeRequest;
import com.pearl.generator.pojo.request.PreviewCodeRequest;
import com.pearl.generator.service.GeneratorCodeService;
import freemarker.cache.ByteArrayTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author TangDan
 * @since 2022-07-07
 */
@Api(value = "GeneratorController", tags = "代码生成")
@RestController
@RequestMapping("/generator")
public class GeneratorCodeController {

    @Resource
    GeneratorCodeService generatorCodeService;
    @ApiOperation(value = "输出代码", produces = "application/json")
    @PostMapping("/batchOutput")
    public Result<Page<DataSourceEntity>> batchOutput(@RequestBody @Validated GeneratorCodeRequest generatorCodeRequest) {
        generatorCodeService.generator(generatorCodeRequest);
        return Result.success();
    }

    @ApiOperation(value = "预览", produces = "application/json")
    @PostMapping("/preview")
    public Result<Map<String,String>> preview(@RequestBody @Validated PreviewCodeRequest request) throws IOException, TemplateException {
        Map<String, String> preview = generatorCodeService.preview(request);
        return Result.success(preview);
    }

}
